﻿using KayakMes.Common.Result;
using KayakMes.RBAC.Application.Input;
using KayakMes.RBAC.Application.Login;
using KayakMes.RBAC.Application.OUT;
using KayakMes.RBAC.Doamin;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Text;
using Yitter.IdGenerator;

namespace KayakMes.RBAC.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IUserService _userService;

        public UserController(IUserService userService)
        {
            _userService = userService;
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public ApiResult<int> AddUser(AddUserInput input)
        {
           return _userService.AddUser(input);

        }
        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        /// 
        [HttpGet]
        public ApiPaging<UserDTO> GetUserAll([FromQuery]GetUserInput input)
        {
           return _userService.GetUserAll(input);
        }
        /// <summary>
        /// 获取用户详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<UserModel> GetUserModel(long Id)
        {
           return _userService.GetUserModel(Id);
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public ApiResult<UserModel> Login(LoginInput input)
        {
            return _userService.Login(input);
        }

        /// <summary>
        /// 导出用户列表
        /// </summary>
        /// <returns>返回Excel文件</returns>
        [HttpGet("export")]
        public async Task<IActionResult> ExportUsers()
        {
            var result = await _userService.Userexport();
            if (result.Code == ApiEnums.Success)
            {
                return File(
                    result.Data,
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                    $"用户列表_{DateTime.Now:yyyyMMddHHmmss}.xlsx"
                );
            }
            return BadRequest(result);
        }
    }
}
